package com.webside.front.index.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webside.front.common.model.BSeries;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.common.model.PBanner;
import com.webside.front.index.service.IndexService;
import com.webside.front.listener.BaseData;
import com.webside.front.search.service.SearchService;
import com.webside.mongo.model.AdRecord;
import com.webside.util.Common;

@Controller
@SessionAttributes(types=PBanner.class)
@RequestMapping(value = "/public/")
public class IndexController {
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private IndexService indexService;
	@Autowired
	private SearchService searchService;

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
 
	
	/**
	 *查询轮播海报和视频
	 * @param m
	 * @return
	 */
	@RequestMapping(value="index.html")
	public String selectAll(Model m,Bdictionary bdictionary){
	       BSeries record=new BSeries();
	       Bdictionary dbict=new Bdictionary(); 
	       SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date=format.format(new Date());
		/*	Map ReleaseAd=new HashMap<String,String>();
			ReleaseAd.put("resource_url", null);
			ReleaseAd.put("link_url", null);
			ReleaseAd.put("ad_interval", null);
			ReleaseAd.put("status", null);*/
			//广告投放
	       List<HashMap<String,Object>> BANNER1=indexService.queryAdByAll(date, "BANNERONE");
	       List<HashMap<String,Object>> BANNER2=indexService.queryAdByAll(date, "BANNERTWO");
	       List<HashMap<String,Object>> BANNER3=indexService.queryAdByAll(date, "BANNERTHREE");
	       List<HashMap<String,Object>> BANNER4=indexService.queryAdByAll(date, "BANNERFOUR");
	       List<HashMap<String,Object>> RECT1=indexService.queryAdByAll(date, "RECTONE");
	       List<HashMap<String,Object>> RECT2=indexService.queryAdByAll(date, "RECTTWO");
	       List<HashMap<String,Object>> RECT3=indexService.queryAdByAll(date, "RECTTHREE");
	       if(BANNER1.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<BANNER1.size();i++){
		    	  
	    	    AdRelease="<a class='banner1' status='"+BANNER1.get(i).get("status")+"' interval='"+BANNER1.get(i).get("ad_interval")+"' href='"+BANNER1.get(i).get("link_url")+"'><img count='0' pcode='"+BANNER1.get(i).get("code")+"' pname='"+BANNER1.get(i).get("name")+"' adid='"+BANNER1.get(i).get("id")+"' adtitle='"+BANNER1.get(i).get("title")+"' adtypename='"+BANNER1.get(i).get("type_name")+"' adtype='"+BANNER1.get(i).get("type")+"' onclick='wordsinsert(this)' width='1198' height='58' src='"+BANNER1.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("banner1_list", lists);
	    	}
	       if(BANNER2.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<BANNER2.size();i++){
		    	  
	    	    AdRelease="<a class='banner2' status='"+BANNER2.get(i).get("status")+"' interval='"+BANNER2.get(i).get("ad_interval")+"' href='"+BANNER2.get(i).get("link_url")+"'><img count='0' pcode='"+BANNER2.get(i).get("code")+"' pname='"+BANNER2.get(i).get("name")+"' adid='"+BANNER2.get(i).get("id")+"' adtitle='"+BANNER2.get(i).get("title")+"' adtypename='"+BANNER2.get(i).get("type_name")+"' adtype='"+BANNER2.get(i).get("type")+"' onclick='wordsinsert(this)' width='1198' height='58' src='"+BANNER2.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("banner2_list", lists);
	    	}
	       if(BANNER3.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<BANNER3.size();i++){
		    	  
	    	    AdRelease="<a class='banner3' status='"+BANNER3.get(i).get("status")+"' interval='"+BANNER3.get(i).get("ad_interval")+"' href='"+BANNER3.get(i).get("link_url")+"'><img count='0' pcode='"+BANNER3.get(i).get("code")+"' pname='"+BANNER3.get(i).get("name")+"' adid='"+BANNER3.get(i).get("id")+"' adtitle='"+BANNER3.get(i).get("title")+"' adtypename='"+BANNER3.get(i).get("type_name")+"' adtype='"+BANNER3.get(i).get("type")+"' onclick='wordsinsert(this)' width='1198' height='58' src='"+BANNER3.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("banner3_list", lists);
	    	}
	       if(BANNER4.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<BANNER4.size();i++){
		    	  
	    	    AdRelease="<a class='banner4' status='"+BANNER4.get(i).get("status")+"' interval='"+BANNER4.get(i).get("ad_interval")+"' href='"+BANNER4.get(i).get("link_url")+"'><img count='0' pcode='"+BANNER4.get(i).get("code")+"' pname='"+BANNER4.get(i).get("name")+"' adid='"+BANNER4.get(i).get("id")+"' adtitle='"+BANNER4.get(i).get("title")+"' adtypename='"+BANNER4.get(i).get("type_name")+"' adtype='"+BANNER4.get(i).get("type")+"' onclick='wordsinsert(this)' width='1198' height='58' src='"+BANNER4.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("banner4_list", lists);
	    	}
	       if(RECT1.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<RECT1.size();i++){
		    	  
	    	    AdRelease="<a class='rect1' status='"+RECT1.get(i).get("status")+"' interval='"+RECT1.get(i).get("ad_interval")+"' href='"+RECT1.get(i).get("link_url")+"'><img count='0' pcode='"+RECT1.get(i).get("code")+"' pname='"+RECT1.get(i).get("name")+"' adid='"+RECT1.get(i).get("id")+"' adtitle='"+RECT1.get(i).get("title")+"' adtypename='"+RECT1.get(i).get("type_name")+"' adtype='"+RECT1.get(i).get("type")+"' onclick='wordsinsert(this)' width='228' height='95' src='"+RECT1.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("rect1_list", lists);
	    	}
	       if(RECT2.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<RECT2.size();i++){
		    	  
	    	    AdRelease="<a class='rect2' status='"+RECT2.get(i).get("status")+"' interval='"+RECT2.get(i).get("ad_interval")+"' href='"+RECT2.get(i).get("link_url")+"'><img count='0' pcode='"+RECT2.get(i).get("code")+"' pname='"+RECT2.get(i).get("name")+"' adid='"+RECT2.get(i).get("id")+"' adtitle='"+RECT2.get(i).get("title")+"' adtypename='"+RECT2.get(i).get("type_name")+"' adtype='"+RECT2.get(i).get("type")+"' onclick='wordsinsert(this)' width='228' height='95' src='"+RECT2.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("rect2_list", lists);
	    	}
	       if(RECT3.size()>0){
	    	   List<String> lists=new ArrayList<String>();
		       String AdRelease="";
	    	   for(int i=0;i<RECT3.size();i++){
		    	  
	    	    AdRelease="<a class='rect3' status='"+RECT3.get(i).get("status")+"' interval='"+RECT3.get(i).get("ad_interval")+"' href='"+RECT3.get(i).get("link_url")+"'><img count='0' pcode='"+RECT3.get(i).get("code")+"' pname='"+RECT3.get(i).get("name")+"' adid='"+RECT3.get(i).get("id")+"' adtitle='"+RECT3.get(i).get("title")+"' adtypename='"+RECT3.get(i).get("type_name")+"' adtype='"+RECT3.get(i).get("type")+"' onclick='wordsinsert(this)' width='228' height='95' src='"+RECT3.get(i).get("resource_url")+"'/></a>";
	    	    lists.add(AdRelease);
	    	   }
	    	   m.addAttribute("rect3_list", lists);
	    	}
	       //查询关键词
		   List<Bdictionary> keywords=indexService.selectKeywords();
			  String []keyarr=keywords.get(0).getName().split(",");
			  m.addAttribute("bdictionaryKeywords_list", keyarr);
	       //查询关键词
		   List<Bdictionary> pageInfo=indexService.selectPageInfo();
		   m.addAttribute("bdictionaryPageInfo", pageInfo);
		   //查询电影的类型
		   List<Bdictionary> ByPrimary=indexService.selectByPrimary(dbict);
		   m.addAttribute("bdictionaryByPrimary_list", ByPrimary);
		   //查询电视剧的类型
		   List<Bdictionary> TVplay=indexService.selectTVplay(dbict);
		   m.addAttribute("bdictionaryTVplay_list", TVplay);
		   //查询动漫的类型
		   List<Bdictionary> Comic=indexService.selectComic(dbict);
		   m.addAttribute("bdictionaryComic_list", Comic);
		   //猜你喜欢
		   List<BSeries> xihun=indexService.selectByPrimarySelective(record);
		   m.addAttribute("xihun_list", xihun);
		   //激情预告
		   List<BSeries> Trailer=indexService.selectByPrimaryTrailer(record);
		   m.addAttribute("Trailer_list", Trailer);
		   
		   List<BSeries> Trailer1=indexService.selectTrailer(record);
		   m.addAttribute("Trailer1", Trailer1);
		    //卫视同步
		   List<BSeries> Satellite=indexService.selectByallSatellite(record);
		   m.addAttribute("Satellite_list", Satellite); 
		   List<BSeries> Satellite1=indexService.selectSatellite(record);
		   m.addAttribute("Satellite1_list", Satellite1);
		    //日韩热播
		   List<BSeries> Korea=indexService.selectByKorea(record);
		   m.addAttribute("Korea_list", Korea);  
		   List<BSeries> Korea1=indexService.selectKorea(record);
		   m.addAttribute("Korea1_list", Korea1);
           //美剧精选
		   List<BSeries> American=indexService.selectAmerican (record); 
		   m.addAttribute("American_list", American);
		   //佳品有约
		   List<BSeries> Jiapin=indexService.selectJiapin(record); 
		   m.addAttribute("Jiapin_list", Jiapin);
		   List<BSeries> ByJiapin=indexService.selectByJiapin(record); 
		   m.addAttribute("ByJiapin_list", ByJiapin);
		   List<BSeries> ByALLJiapin=indexService.selectByALLJiapin(record); 
		   m.addAttribute("ByALLJiapin_list", ByALLJiapin);
		   //影院大片
		   List<BSeries> Cinema=indexService.selectCinema(record); 
		   m.addAttribute("Cinema_list", Cinema);
		   //动漫剧场
		   List<BSeries> Animation=indexService.selectByAnimation(record); 
		   m.addAttribute("Animation_list", Animation);
		   List<BSeries> Mation=indexService.selectAnimation(record);
		   m.addAttribute("Mation_list", Mation);
		   //电视剧榜
		   List<BSeries> play=indexService.selectplay(record); 
		   m.addAttribute("play_list", play);
		   //电视剧影迷力荐
	       List<BSeries> ByTvplay=indexService.selectByTvplayALL(); 
	       m.addAttribute("ByTvplay_list", ByTvplay);
		   //佳品电影榜
		   List<BSeries> Jiapinplay=indexService.selectJiapinplay(record); 
		   m.addAttribute("Jiapinplay_list", Jiapinplay);
		   //佳品电影影迷力荐
	       List<BSeries> LikeCounts=indexService.selectByLikeCount(); 
	       m.addAttribute("LikeCount_list", LikeCounts);
		   
		   //影迷力荐
	       List<BSeries> BSeries=indexService.selectByPrimaryALL(); 
	       m.addAttribute("BSeries_list", BSeries);
	      
		   //查询轮播图
	       List<PBanner> list=indexService.getAllSubject();
	       m.addAttribute("banner_list", list);
	       //查询轮播图广告
	       List<HashMap<String,Object>> FOCUS=indexService.queryAdByAll(date, "FOCUSONE");
	       if(FOCUS.size()>0){
	    	   for(int i=0;i<FOCUS.size();i++){
	    		   PBanner pBanner=new PBanner();
	    		   list.add(pBanner);
	    		   list.get(list.size()-1).setImgUrl(FOCUS.get(i).get("resource_url").toString());
	    		   list.get(list.size()-1).setLinkUrl(FOCUS.get(i).get("link_url").toString());
	    		   list.get(list.size()-1).setTarget("count='0' pcode='"+FOCUS.get(i).get("code")+"' pname='"+FOCUS.get(i).get("name")+"' adid='"+FOCUS.get(i).get("id")+"' adtitle='"+FOCUS.get(i).get("title")+"' adtypename='"+FOCUS.get(i).get("type_name")+"' adtype='"+FOCUS.get(i).get("type")+"' onclick='wordsinsert(this)'");
	    		   
	    	   }
	       }
	       //查询合作伙伴
	       List<PBanner> Targets=indexService.getAllTarget();
	       m.addAttribute("Target_list", Targets);
	       m.addAttribute("baseImgPath",BaseData.img_base_path);
	       
	       
	    return Common.BACKGROUND_PATH + "/front/index";
	}
	@RequestMapping("addAdRecord.html")
	public void addAdRecord(AdRecord adRecord,HttpServletRequest request){
		adRecord.setBrowerTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		adRecord.setBrowerDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		adRecord.setIp(Common.toIpAddr(request));
		// 调用java自带的uuid生成唯一标示id
		UUID uuid = UUID.randomUUID();
		// 去掉不需要的字符
		String id = uuid.toString().replaceAll("\\-", "");
		// 为实体对象加入标示id
		adRecord.setId(id);
		adRecord.setSessionId(request.getSession().getId());
		mongoTemplate.insert(adRecord, "ad_brower_records");
	}
}
